package com.bt.mes.brm.provider.service;

import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_INQUIRE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_LIST_SWH;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_LIST_WHOLE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_NEW;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISPTH_NOT_FOUND_RECORDER;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISPTH_PATH_ITEM_ALREADY_EXIST;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISPTH_PATH_ITEM_NOT_FOUND;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISPTH_RECORDER_ALREADY_EXIST;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.bt.mes.brm.api.service.IXpbispathService;
import com.bt.mes.brm.provider.tx.Xpbispth.XpbispthI;
import com.bt.mes.brm.provider.tx.Xpbispth.XpbispthIA;
import com.bt.mes.brm.provider.tx.Xpbispth.XpbispthIB;
import com.bt.mes.brm.provider.tx.Xpbispth.XpbispthO;
import com.bt.mes.brm.provider.tx.Xpbispth.XpbispthOA;
import com.bt.mes.brm.provider.tx.Xpbispth.XpbispthOB;
import com.bt.mes.brm.provider.tx.Xpbistol.XpbistolI;
import com.bt.mes.common.dao.IBisPathDao;
import com.bt.mes.common.dao.IBisPathSwhDao;
import com.bt.mes.common.dao.IBisPathiDao;
import com.bt.mes.common.dao.IRetPrdInfoDao;
import com.bt.mes.common.dao.IRetWODao;
import com.bt.mes.common.util.JacksonUtil;
import com.bt.mes.common.util.MqOpe;
import com.bt.table.ppt.Bis_path;
import com.bt.table.ppt.Bis_pathId;
import com.bt.table.ppt.Bis_path_swh;
import com.bt.table.ppt.Bis_pathi;
import com.bt.table.ppt.Bis_pathiId;
import com.bt.table.ppt.Ret_prd_info;
import com.bt.table.ppt.Ret_wo;

@Service("xpbispathService")
public class XpbispathService implements IXpbispathService {
	private Logger logger = Logger.getLogger(XpbispathService.class);
	private XpbispthI inTrx;

	private XpbispthO outTrx;

	@Autowired
	private IBisPathDao bisPathDao;

	@Autowired
	private IBisPathiDao bisPathiDao;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IBisPathSwhDao bisPathSwhDao;
	@Autowired
	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		String strOutTrx = "";
		logger.info("InTrx is " + strInMsg);
		Session session = bisPathDao.getCrSession();

		inTrx = new XpbispthI();
		outTrx = new XpbispthO();
		outTrx.setTrx_id("XPBISPTH");
		outTrx.setType_id("O");
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, XpbispthI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition();
				break;
			case ACTION_FLG_ADD:
				save();
				break;
			case ACTION_FLG_UPDATE:
				updateFnc();
				break;
			case ACTION_FLG_DELETE:
				deleteFnc();
				break;
			case ACTION_FLG_INQUIRE:
				queryPathItemFnc();
				break;
			case ACTION_FLG_NEW:
				addORUpdatePathItemFnc();
				break;
			case ACTION_FLG_LIST_WHOLE:
				listWholeFnc();
				break;
			case ACTION_FLG_LIST_SWH:
				querySwhPath();
				break;
			case 'B':
				queryRWKByCondition();
				break;
			default:
				errActionFnc();
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logger.info(strOutTrx);
		}
		return strOutTrx;
	}

	public void save(){
		addFnc();
		addORUpdatePathItemFnc();
		//TODO:nodes,connecter ..
	}
	private void queryRWKByCondition() {
		List<XpbispthOA> oaryAList = new ArrayList<XpbispthOA>();
		//
		// if(xpbispthI.getIaryB()==null){
		// return ;
		// }
		// XpbispthIB iaryB = xpbispthI.getIaryB().get(0);
		String wo_id = inTrx.getWo_id_fk();
		String prd_seq_id = inTrx.getPrd_seq_id();

		Ret_wo ret_wo = new Ret_wo();
		ret_wo = retWODao.get(wo_id);
		if (ret_wo == null) {
			return;
		}

		Ret_prd_info ret_prd_info = new Ret_prd_info();
		ret_prd_info = retPrdInfoDao.get(prd_seq_id);
		if (ret_prd_info == null) {
			return;
		}
		if (!ret_prd_info.getPrd_stat().equals("INPR")) {
			return;
		}

		Bis_path bis_path = new Bis_path();
		Bis_pathId bis_pathId = new Bis_pathId();

		String fr_path_id_fk = ret_prd_info.getCr_path_id_fk();
		String fr_path_ver_fk = ret_prd_info.getCr_path_ver_fk();
		String fr_ope_no_fk = ret_prd_info.getCr_ope_no_fk();

		bis_pathId.setPath_id_fk(fr_path_id_fk);
		bis_pathId.setPath_ver_fk(fr_path_ver_fk);
		bis_pathId.setCr_ope_no(fr_ope_no_fk);

		bis_path = bisPathDao.get(bis_pathId);
		if (null != bis_path) {
			if (bis_path.getSwh_avl_flg().equals("N")) {
				outTrx.setOaryA(null);
				outTrx.setTbl_cnt_a(0);
				return;
			}
		}

		List<Bis_path_swh> bis_path_swhs = new ArrayList<Bis_path_swh>();

		String hql = "FROM Bis_path_swh WHERE fr_path_id_fk = '" + fr_path_id_fk + "' AND fr_path_ver_fk = '"
				+ fr_path_ver_fk + "' AND fr_ope_no_fk = '" + fr_ope_no_fk + "'";

		bis_path_swhs = bisPathSwhDao.find(hql);

		if (!bis_path_swhs.isEmpty()) {
			for (Bis_path_swh path_swh : bis_path_swhs) {
				String sql = "FROM Bis_pathi where PATH_ID = '" + path_swh.getSwh_path_id_fk() + "' and PATH_VER = '"
						+ path_swh.getSwh_path_ver_fk() + "'";
				List<Bis_pathi> bis_pathi = bisPathiDao.find(sql);
				XpbispthOA oaryA = new XpbispthOA();
				oaryA.setPath_id(path_swh.getSwh_path_id_fk());
				oaryA.setPath_ver(path_swh.getSwh_path_ver_fk());
				oaryA.setPath_dsc(path_swh.getSwh_dsc());
				if (!bis_pathi.isEmpty()) {
					oaryA.setFirst_ope_no(bis_pathi.get(0).getFirst_ope_no());
				}
				oaryAList.add(oaryA);
			}
			outTrx.setOaryA(oaryAList);
			outTrx.setTbl_cnt_a(oaryAList.size());
		} else {
			outTrx.setOaryA(null);
			outTrx.setTbl_cnt_a(0);
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(
					"工艺线路[" + fr_path_id_fk + "],当前站点[" + fr_ope_no_fk + "],在Bis_path_swh中不存在，请至6100界面维护分支信息");
		}

		/*
		 * List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
		 * String sql =
		 * "select path_id,path_ver,path_dsc from BIS_PATHI where PATH_CATE = 'REWK' "
		 * ; sql = sql +
		 * "and FIRST_OPE_NO in (select PV_OPE_NO from BIS_PATH where PATH_ID_FK = '"
		 * +ret_wo.getPath_id_fk()
		 * +"' and PATH_VER_FK = '"+ret_wo.getPath_ver_fk()
		 * +"' and CR_OPE_NO = '"+ret_prd_info.getCr_ope_no_fk()+"')";
		 * 
		 * List<Object> list = retPrdInfoDao.findBySQL(sql); if
		 * (!list.isEmpty()) { for (int i=0;i<list.size();i++){ Object[] obj =
		 * (Object[])list.get(i); XpbispthOA oaryA = new XpbispthOA();
		 * oaryA.setPath_id(obj[0].toString());
		 * oaryA.setPath_ver(obj[1].toString());
		 * oaryA.setPath_dsc(obj[2].toString()); oaryAList.add(oaryA); }
		 * xpbispthO.setOaryA(oaryAList);
		 * xpbispthO.setTbl_cnt_a(oaryAList.size()); }
		 */

		// String hql = "FROM Bis_path where 1=1";
		// if(path_id!=null){
		// hql = hql + "and path_id_fk = '"+path_id+"'";
		// }
		// if(path_ver!=null){
		// hql = hql + "and path_ver_fk = '"+path_ver+"'";
		// }
		// if(ope_no!=null){
		// hql = hql + "and nx_ope_no = '"+ope_no+"'";
		// }
		// List<Bis_path> bisPathList = bisPathDao.find(hql);
		// for(Bis_path bis_path:bisPathList){
		// String rhql = "FROM BIS_PATHI where first_ope_no =
		// '"+bis_path.getId().getCr_ope_no()+"'";
		// List<Bis_pathi> bisRWKPathList = bisPathiDao.find(rhql);
		// for(Bis_pathi bis_pathi:bisRWKPathList){
		// XpbispthOB oaryB = new XpbispthOB();
		// oaryB.setPath_id_fk(bis_pathi.getId().getPath_id());
		// oaryB.setPath_ver_fk(bis_pathi.getId().getPath_ver());
		// oaryB.setCr_ope_no(bis_pathi.getFirst_ope_no());
		// oaryBList.add(oaryB);
		// }
		// }
		// xpbispthO.setOaryB(oaryBList);
		// xpbispthO.setTbl_cnt_b(oaryBList.size());
		// if(path_cate!=null){
		// if(path_cate.indexOf('*')==-1){
		// hql = hql + " and path_cate='" + path_cate + "'";
		// }else{
		// hql = hql + " and path_cate like '" + path_cate.replace('*', '%') +
		// "'";
		// }
		// }
		// if(mdl_cate!=null){
		// hql = hql + " and mdl_cate_fk = '" + mdl_cate + "'";
		// }
		// List<Bis_pathi> bisPathiList = bisPathiDao.find(hql);
		// for(Bis_pathi bis_pathi : bisPathiList ){
		// XpbispthOA oaryA = new XpbispthOA();
		// oaryA.setPath_id(bis_pathi.getId().getPath_id());
		// oaryA.setPath_ver(bis_pathi.getId().getPath_ver());
		// oaryA.setPath_dsc(bis_pathi.getPath_dsc());
		// oaryA.setPath_cate(bis_pathi.getPath_cate());
		// oaryA.setFirst_ope_no(bis_pathi.getFirst_ope_no());
		// oaryA.setStr_bank_id(bis_pathi.getStr_bank_id());
		// oaryA.setEnd_bank_id(bis_pathi.getEnd_bank_id());
		// oaryA.setMax_swh_cnt(bis_pathi.getMax_swh_cnt());
		// oaryA.setMdl_cate(bis_pathi.getMdl_cate_fk());
		// oaryAList.add(oaryA);
		// }
		// xpbispthO.setOaryA(oaryAList);
		// xpbispthO.setTbl_cnt_a(oaryAList.size());
	}

	public void queryByCondition() {
		if (inTrx.getIaryA() == null) {
			return;
		}
		XpbispthIA iaryA = inTrx.getIaryA().get(0);
		List<XpbispthOA> oaryAList = new ArrayList<XpbispthOA>();
		List<XpbispthOB> oaryBList = new ArrayList<XpbispthOB>();
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		String path_cate = iaryA.getPath_cate();
		String mdl_cate = iaryA.getMdl_cate();
		String hql = "FROM Bis_pathi where 1=1";
		if (path_id != null) {
			if (path_id.indexOf('*') == -1) {
				hql = hql + " and id.path_id in ('" + path_id + "')";
			} else {
				hql = hql + " and id.path_id like '" + path_id.replace('*', '%') + "'";
			}
		}
		if (path_ver != null) {
			if (path_ver.indexOf('*') == -1) {
				hql = hql + " and id.path_ver='" + path_ver + "'";
			} else {
				hql = hql + " and id.path_ver like '" + path_ver.replace('*', '%') + "'";
			}
		}
		if (path_cate != null) {
			if (path_cate.indexOf('*') == -1) {
				hql = hql + " and path_cate='" + path_cate + "'";
			} else {
				hql = hql + " and path_cate like '" + path_cate.replace('*', '%') + "'";
			}
		}
		if (mdl_cate != null) {
			hql = hql + " and mdl_cate_fk = '" + mdl_cate + "'";
		}
		if (iaryA.getMax_swh_cnt() != null) {
			hql = hql + " and max_swh_cnt = " + iaryA.getMax_swh_cnt() + "";
			// }else{
			// hql = hql + " and max_swh_cnt is null ";
		}
		List<Bis_pathi> bisPathiList = bisPathiDao.find(hql);
		for (Bis_pathi bis_pathi : bisPathiList) {
			XpbispthOA oaryA = new XpbispthOA();
			oaryA.setPath_id(bis_pathi.getId().getPath_id());
			oaryA.setPath_ver(bis_pathi.getId().getPath_ver());
			oaryA.setPath_dsc(bis_pathi.getPath_dsc());
			oaryA.setPath_cate(bis_pathi.getPath_cate());
			oaryA.setFirst_ope_no(bis_pathi.getFirst_ope_no());
			oaryA.setStr_bank_id(bis_pathi.getStr_bank_id());
			oaryA.setEnd_bank_id(bis_pathi.getEnd_bank_id());
			oaryA.setMax_swh_cnt(bis_pathi.getMax_swh_cnt());
			oaryA.setMdl_cate(bis_pathi.getMdl_cate_fk());
			oaryAList.add(oaryA);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
		if (outTrx.getTbl_cnt_a() > 0) {
			String hql_dt = "FROM Bis_path where id.path_id_fk = '" + outTrx.getOaryA().get(0).getPath_id()
					+ "' and id.path_ver_fk = '" + outTrx.getOaryA().get(0).getPath_ver() + "'";
			List<Bis_path> bisPathList = bisPathDao.find(hql_dt);
			for (Bis_path bis_path : bisPathList) {
				XpbispthOB oaryB = new XpbispthOB();
				oaryB.setPath_id_fk(bis_path.getId().getPath_id_fk());
				oaryB.setPath_ver_fk(bis_path.getId().getPath_ver_fk());
				oaryB.setCr_ope_no(bis_path.getId().getCr_ope_no());
				oaryB.setPv_ope_no(bis_path.getPv_ope_no());
				oaryB.setNx_ope_no(bis_path.getNx_ope_no());
				oaryB.setCr_ope_id_fk(bis_path.getCr_ope_id_fk());
				oaryB.setCr_ope_ver_fk(bis_path.getCr_ope_ver_fk());
				oaryB.setSwh_rst_flg(bis_path.getSwh_rst_flg());
				oaryB.setSwh_avl_flg(bis_path.getSwh_avl_flg());
				oaryB.setWip_bank_flg(bis_path.getWip_bank_flg());
				oaryB.setDft_wip_bank_tool_id_fk(bis_path.getDft_wip_bank_tool_id_fk());
				oaryBList.add(oaryB);
			}
			outTrx.setOaryB(oaryBList);
			outTrx.setTbl_cnt_b(oaryBList.size());
		}
	}

	public void addFnc() {
		XpbispthIA iaryA = inTrx.getIaryA().get(0);
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(path_id);
		id.setPath_ver(path_ver);
		Bis_pathi query_bis_pathi = bisPathiDao.get(id);
		if (query_bis_pathi != null) {
			outTrx.setRtn_code(E_XPBISPTH_RECORDER_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("record already exist");
			return;
		}
		Bis_pathi bis_pathi = new Bis_pathi();
		bis_pathi.setId(id);
		bis_pathi.setPath_cate(iaryA.getPath_cate());
		bis_pathi.setPath_dsc(iaryA.getPath_dsc());
		bis_pathi.setFirst_ope_no(iaryA.getFirst_ope_no());
		bis_pathi.setStr_bank_id(iaryA.getStr_bank_id());
		bis_pathi.setEnd_bank_id(iaryA.getEnd_bank_id());
		bis_pathi.setMax_swh_cnt(iaryA.getMax_swh_cnt());
		bis_pathi.setMdl_cate_fk(iaryA.getMdl_cate());
		bisPathiDao.save(bis_pathi);
	}

	public void updateFnc() {
		XpbispthIA iaryA = inTrx.getIaryA().get(0);
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(path_id);
		id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(id);
		if (bis_pathi != null) {
			outTrx.setRtn_code(E_XPBISPTH_NOT_FOUND_RECORDER + "");
			outTrx.setRtn_mesg("record already exist");
			return;
		}
		if (iaryA.getPath_cate() != null) {
			bis_pathi.setPath_cate(iaryA.getPath_cate());
		}
		if (iaryA.getPath_dsc() != null) {
			bis_pathi.setPath_dsc(iaryA.getPath_dsc());
		}
		if (iaryA.getFirst_ope_no() != null) {
			bis_pathi.setFirst_ope_no(iaryA.getFirst_ope_no());
		}
		if (iaryA.getStr_bank_id() != null) {
			bis_pathi.setStr_bank_id(iaryA.getStr_bank_id());
		}
		if (iaryA.getEnd_bank_id() != null) {
			bis_pathi.setEnd_bank_id(iaryA.getEnd_bank_id());
		}
		if (iaryA.getMax_swh_cnt() != null) {
			bis_pathi.setMax_swh_cnt(iaryA.getMax_swh_cnt());
		}
		bisPathiDao.update(bis_pathi);
	}

	public void deleteFnc() {
		XpbispthIA iaryA = inTrx.getIaryA().get(0);
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(path_id);
		id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(id);
		if (bis_pathi == null) {
			outTrx.setRtn_code(E_XPBISPTH_NOT_FOUND_RECORDER + "");
			outTrx.setRtn_mesg("record already exist");
			return;
		}
		bisPathiDao.delete(bis_pathi);
		// 将route 站点信息删除
		String hql = "FROM Bis_path where path_id_fk='" + path_id + "'" + " and path_ver_fk ='" + path_ver + "'";
		List<Bis_path> pathItemList = bisPathDao.find(hql);
		for (Bis_path bis_path : pathItemList) {
			bisPathDao.delete(bis_path);
		}
	}

	public void queryPathItemFnc() {
		queryByCondition();
		XpbispthIB iaryB = inTrx.getIaryB().get(0);
		List<XpbispthOB> oaryBList = new ArrayList<XpbispthOB>();
		String cr_ope_no = iaryB.getCr_ope_no();
		String path_id = iaryB.getPath_id_fk();
		String path_ver = iaryB.getPath_ver_fk();
		String hql = "FROM Bis_path where 1=1";
		if (path_id != null) {
			if (path_id.indexOf('*') == -1) {
				hql = hql + " and id.path_id_fk='" + path_id + "'";
			} else {
				hql = hql + " and id.path_id_fk like '" + path_id.replace('*', '%') + "'";
			}
		}
		if (path_ver != null) {
			if (path_ver.indexOf('*') == -1) {
				hql = hql + " and id.path_ver_fk='" + path_ver + "'";
			} else {
				hql = hql + " and id.path_ver_fk like '" + path_ver.replace('*', '%') + "'";
			}
		}
		if (cr_ope_no != null) {
			if (cr_ope_no.indexOf('*') == -1) {
				hql = hql + " and id.cr_ope_no='" + cr_ope_no + "'";
			} else {
				hql = hql + " and id.cr_ope_no like '" + cr_ope_no.replace('*', '%') + "'";
			}
		}
		List<Bis_path> bisPathList = bisPathDao.find(hql);
		for (Bis_path bis_path : bisPathList) {
			XpbispthOB oaryB = new XpbispthOB();
			oaryB.setPath_id_fk(bis_path.getId().getPath_id_fk());
			oaryB.setPath_ver_fk(bis_path.getId().getPath_ver_fk());
			oaryB.setCr_ope_no(bis_path.getId().getCr_ope_no());
			oaryB.setPv_ope_no(bis_path.getPv_ope_no());
			oaryB.setNx_ope_no(bis_path.getNx_ope_no());
			oaryB.setCr_ope_id_fk(bis_path.getCr_ope_id_fk());
			oaryB.setCr_ope_ver_fk(bis_path.getCr_ope_ver_fk());
			oaryB.setSwh_rst_flg(bis_path.getSwh_rst_flg());
			oaryB.setSwh_avl_flg(bis_path.getSwh_avl_flg());
			oaryB.setWip_bank_flg(bis_path.getWip_bank_flg());
			oaryB.setDft_wip_bank_tool_id_fk(bis_path.getDft_wip_bank_tool_id_fk());
			oaryBList.add(oaryB);
		}
		outTrx.setOaryB(oaryBList);
		outTrx.setTbl_cnt_b(oaryBList.size());

	}

	public void addORUpdatePathItemFnc() {
		// 删除route信息 重写insert

		// XpbispthIB iaryB = xpbispthI.getIaryB().get(0);
		List<XpbispthOB> oaryBList = new ArrayList<XpbispthOB>();
		String path_id = inTrx.getIaryB().get(0).getPath_id_fk();
		String path_ver = inTrx.getIaryB().get(0).getPath_ver_fk();
		Bis_pathiId pathi_id = new Bis_pathiId();
		pathi_id.setPath_id(path_id);
		pathi_id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(pathi_id);
		if (bis_pathi == null) {
			addFnc();
			// xpbispthO.setRtn_code( E_XPBISPTH_PATH_INFO_NOT_FOUND + "" );
			// xpbispthO.setRtn_mesg( "Path Info not found" );
			// return ;
		}
		// 更新pathi.first_ope_no
		String first_ope_no = inTrx.getIaryA().get(0).getFirst_ope_no();
		bis_pathi.setFirst_ope_no(first_ope_no);
		bisPathiDao.update(bis_pathi);

		// 将route 站点信息删除
		String hql = "FROM Bis_path where path_id_fk='" + path_id + "'" + " and path_ver_fk ='" + path_ver + "'";
		List<Bis_path> pathItemList = bisPathDao.find(hql);
		for (Bis_path bis_path : pathItemList) {
			bisPathDao.delete(bis_path);
		}
		// 将站点信息重新写入path表
		long tbl_cnt_b = inTrx.getTbl_cnt_b();
		for (int i = 0; i < tbl_cnt_b; i++) {
			XpbispthIB iaryB = inTrx.getIaryB().get(i);
			String cr_ope_no = iaryB.getCr_ope_no();
			String path_id_fk = iaryB.getPath_id_fk();
			String path_ver_fk = iaryB.getPath_ver_fk();
			Bis_pathId id = new Bis_pathId();
			id.setCr_ope_no(cr_ope_no);
			id.setPath_id_fk(path_id_fk);
			id.setPath_ver_fk(path_ver_fk);
			Bis_path query_bis_path = bisPathDao.get(id);
			if (query_bis_path != null) {
				outTrx.setRtn_code(E_XPBISPTH_PATH_ITEM_ALREADY_EXIST + "");
				outTrx.setRtn_mesg("recorder already exist");
				return;
			}
			Bis_path bis_path = new Bis_path();
			bis_path.setId(id);
			bis_path.setCr_ope_id_fk(iaryB.getCr_ope_id_fk());
			bis_path.setCr_ope_ver_fk(iaryB.getCr_ope_ver_fk());
			bis_path.setDft_wip_bank_tool_id_fk(iaryB.getDft_wip_bank_tool_id_fk());
			bis_path.setNx_ope_no(iaryB.getNx_ope_no());
			bis_path.setPv_ope_no(iaryB.getPv_ope_no());
			bis_path.setSwh_avl_flg(iaryB.getSwh_avl_flg());
			bis_path.setSwh_rst_flg(iaryB.getSwh_rst_flg());
			bis_path.setWip_bank_flg(iaryB.getWip_bank_flg());
			bisPathDao.save(bis_path);
		}

	}
	
	private void saveNodes(){
		
	}

	public void updatePathItemFnc() {
		XpbispthIB iaryB = inTrx.getIaryB().get(0);
		List<XpbispthOB> oaryBList = new ArrayList<XpbispthOB>();
		String cr_ope_no = iaryB.getCr_ope_no();
		String path_id_fk = iaryB.getPath_id_fk();
		String path_ver_fk = iaryB.getPath_ver_fk();
		Bis_pathId id = new Bis_pathId();
		id.setCr_ope_no(cr_ope_no);
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		Bis_path bis_path = bisPathDao.get(id);
		if (bis_path != null) {
			outTrx.setRtn_code(E_XPBISPTH_PATH_ITEM_NOT_FOUND + "");
			outTrx.setRtn_mesg("recorder already exist");
			return;
		}
		if (iaryB.getCr_ope_id_fk() != null) {
			bis_path.setCr_ope_id_fk(iaryB.getCr_ope_id_fk());
		}
		if (iaryB.getCr_ope_ver_fk() != null) {
			bis_path.setCr_ope_ver_fk(iaryB.getCr_ope_ver_fk());
		}
		if (iaryB.getDft_wip_bank_tool_id_fk() != null) {
			bis_path.setDft_wip_bank_tool_id_fk(iaryB.getDft_wip_bank_tool_id_fk());
		}
		if (iaryB.getNx_ope_no() != null) {
			bis_path.setNx_ope_no(iaryB.getNx_ope_no());
		}
		if (iaryB.getPv_ope_no() != null) {
			bis_path.setPv_ope_no(iaryB.getPv_ope_no());
		}
		if (iaryB.getSwh_avl_flg() != null) {
			bis_path.setSwh_avl_flg(iaryB.getSwh_avl_flg());
		}
		if (iaryB.getSwh_rst_flg() != null) {
			bis_path.setSwh_rst_flg(iaryB.getSwh_rst_flg());
		}
		if (iaryB.getWip_bank_flg() != null) {
			bis_path.setWip_bank_flg(iaryB.getWip_bank_flg());
		}

		bisPathDao.update(bis_path);
	}

	public void deletePathItemFnc() {
		XpbispthIB iaryB = inTrx.getIaryB().get(0);
		String cr_ope_no = iaryB.getCr_ope_no();
		String path_id_fk = iaryB.getPath_id_fk();
		String path_ver_fk = iaryB.getPath_ver_fk();
		Bis_pathId id = new Bis_pathId();
		id.setCr_ope_no(cr_ope_no);
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		Bis_path bis_path = bisPathDao.get(id);
		if (bis_path != null) {
			outTrx.setRtn_code(E_XPBISPTH_PATH_ITEM_NOT_FOUND + "");
			outTrx.setRtn_mesg("recorder already exist");
			return;
		}
		bisPathDao.delete(bis_path);
	}

	public void listWholeFnc() {
		List<XpbispthOA> oaryAList = new ArrayList<XpbispthOA>();
		String hql = "FROM Bis_pathi";
		List<Bis_pathi> pathiList = bisPathiDao.find(hql);
		for (Bis_pathi bis_pathi : pathiList) {
			XpbispthOA oary = new XpbispthOA();
			oary.setPath_id(bis_pathi.getId().getPath_id());
			oary.setPath_ver(bis_pathi.getId().getPath_ver());
			oary.setPath_cate(bis_pathi.getPath_cate());
			oary.setPath_dsc(bis_pathi.getPath_dsc());
			oary.setFirst_ope_no(bis_pathi.getFirst_ope_no());
			oary.setStr_bank_id(bis_pathi.getStr_bank_id());
			oary.setEnd_bank_id(bis_pathi.getEnd_bank_id());
			oary.setMax_swh_cnt(bis_pathi.getMax_swh_cnt());
			oaryAList.add(oary);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
	}

	public void errActionFnc() {
		outTrx.setTrx_id("XPBISPTH");
		outTrx.setType_id("O");
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}

	public void querySwhPath() {
		List<XpbispthOA> oaryAList = new ArrayList<XpbispthOA>();
		String hql = "FROM Bis_pathi where path_cate = 'REWK'";
		List<Bis_pathi> pathiList = bisPathiDao.find(hql);
		for (Bis_pathi bis_pathi : pathiList) {
			XpbispthOA oary = new XpbispthOA();
			oary.setPath_id(bis_pathi.getId().getPath_id());
			oary.setPath_ver(bis_pathi.getId().getPath_ver());
			oary.setPath_cate(bis_pathi.getPath_cate());
			oary.setPath_dsc(bis_pathi.getPath_dsc());
			oary.setFirst_ope_no(bis_pathi.getFirst_ope_no());
			oary.setStr_bank_id(bis_pathi.getStr_bank_id());
			oary.setEnd_bank_id(bis_pathi.getEnd_bank_id());
			oary.setMax_swh_cnt(bis_pathi.getMax_swh_cnt());
			oaryAList.add(oary);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
	}

}
